package com.nebula.common.util;

import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;


public class MtimeBeanUtils {
	
	/**
	 * 深度拷贝list里面bean的值
	 * @param <B>
	 * @param <T>
	 * @param sourceList
	 * @param targetList
	 * @param targetBeanCls
	 */
	public static <T,B> void copyListBeanProperties(List<T> sourceList,List<B> targetList,Class<B> targetBeanCls ){
		for (Object t : sourceList) {
			B b=createInstance(targetBeanCls);
			BeanUtils.copyProperties(t, b);
			targetList.add(b);
		}
	}
	
	/**
	 * 深度拷贝list里面bean的值
	 * @param sourceList
	 * @param targetBeanCls
	 * @return
	 */
	public static <T,B> List<B> copyListBeanProperties(List<T> sourceList,Class<B> targetBeanCls ){
		List<B> targetList=new ArrayList<>();
		for (Object t : sourceList) {
			B b=createInstance(targetBeanCls);
			BeanUtils.copyProperties(t, b);
			targetList.add(b);
		}
		return targetList;
	}
	
	/**
	 * 拷贝bean的属性
	 * @param source
	 * @param target
	 */
	public static <T,B> void copyBeanProperties(T source,B target){
		BeanUtils.copyProperties(source, target);
	}
	
	/**
	 * 拷贝bean的属性
	 * @param source
	 * @param targetBeanCls
	 * @return
	 */
	public static <T,B> B copyBeanProperties(T source,Class<B> targetBeanCls ){
		if(null!=source){
			B target=createInstance(targetBeanCls);
			BeanUtils.copyProperties(source, target);
			return target;
		}else{
			return null;
		}
		
		
	}
	
	
	private static <B> B createInstance(Class<B> cls) {
         B obj=null;
         try {
             obj=cls.newInstance();
         } catch (Exception e) {
             obj=null;
         }
         return obj;
     }
	
	
	
}
